<template>
  <div>
    <h1>新建分类</h1>
    <el-form label-width="80px" @submit.native.prevent="save">
      <el-form-item label="所属分类">
        <el-select v-model="formInfo.parents" clearable>
          <el-option
            v-for="item in parentOptions"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="formInfo.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { CATEGORY } from '@/utils/const'

export default {
  mixins: [commonMixin],
  data () {
    return {
      // 上级分类数据
      parentOptions: {}
    }
  },
  created () {
    this.fetchParentOptions()
  },
  methods: {
    save () {
      this.add(CATEGORY)
    },
    async fetchParentOptions () {
      const res = await this.$api.getList(CATEGORY)
      this.parentOptions = res.data
      // console.log(res)
    }
  },
}
</script>

<style scoped>
</style>